﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ajayumi.develop.win32.Transport.AppUpgrade.Properties;
using ajayumi.develop.win32.Transport.Utils;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Provider
{
    /// <summary>
    /// ZipUpdateProvider.cs
    /// Author   : 黄乙冬
    /// Date     : 2012-02-24
    /// </summary>
    internal class ZipProvider : AppPackageProvider
    {

        protected Action<string> DisplayAction { get; private set; }

        public ZipProvider()
            : base(string.Empty)
        {
            // TODO: Complete member initialization
        }
        public ZipProvider(string updateFileDir, Action<string> displayAction)
            : base(updateFileDir)
        {
            // TODO: Complete member initialization
            this.DisplayAction = displayAction;
        }


        public override bool Update()
        {
            try
            {
                string zipPath = Utility.GetBakPath();
                if (File.Exists(zipPath))
                { File.Delete(zipPath); }

                // 压缩备份文件
                FastZipEvents events = new FastZipEvents();
                events.Progress = (obj, evt) =>
                {
                    string str = string.Format("{0} 备份进度：{1}%\r\n", evt.Name, evt.PercentComplete);
                    this.DisplayAction(str);
                };
                events.CompletedFile = (obj, evt) =>
                {
                    string str = string.Format("{0} 备份{1}\r\n", evt.Name, (evt.ContinueRunning ? "成功" : "失败"));
                    this.DisplayAction(str);
                };
                FastZip fastZip = new FastZip(events);
                fastZip.CreateZip(zipPath,
                    ArgumentSerialization.AppDirectory,
                    true,
                    Settings.Default.FileFilter,
                    Settings.Default.DirectoryFilter);

                bool isInvalidZip = false;
                using (ZipFile zip = new ZipFile(zipPath))
                {
                    isInvalidZip = (zip.Count <= 0);
                }
                if (isInvalidZip)
                { File.Delete(zipPath); }

                // 更新
                string[] upgradeFiles = FileHelper.GetSubFiles(this.UpdateFileDir);
                this.CopyTo(upgradeFiles, AppFileAction.UpdateFile);

                try
                {
                    // 删除更新文件
                    if (Directory.Exists(this.UpdateFileDir))
                    { Directory.Delete(this.UpdateFileDir, true); }
                }
                catch (Exception)
                {
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override bool Restore()
        {
            try
            {
                string zipPath = Utility.GetBakPath();
                if (File.Exists(zipPath))
                {
                    (new FastZip()).ExtractZip(zipPath, ArgumentSerialization.AppDirectory, string.Empty);
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected override void CopyTo(IEnumerable<string> files, AppFileAction action)
        {
            int length = files.Count();

            foreach (string item in files)
            {
                string path = string.Empty;
                switch (action)
                {
                    case AppFileAction.RestoreFile:
                        path = item.Replace(this.BackupDir, ArgumentSerialization.AppDirectory);
                        break;
                    case AppFileAction.BackupFile:
                        path = item.Replace(ArgumentSerialization.AppDirectory, this.BackupDir);
                        break;
                    case AppFileAction.UpdateFile:
                        path = item.Replace(this.UpdateFileDir, ArgumentSerialization.AppDirectory);
                        break;
                }

                FileHelper.CreateDir(path);
                File.Copy(item, path, true);

                this.DisplayAction(string.Format("{0} 复制到 {1}\r\n", item, path));
            }
        }


    }
}
